package com.web.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.web.dao.TUserDao;
import com.web.model.PageBean;
import com.web.model.TUser;
import com.web.service.TUserService;
import com.web.utils.MD5;
import com.web.utils.Pager;
import com.web.utils.WebUtil;

/**
 * @title:TUserServiceImpl
 * @Description:用户Service实现
 * @email 223450427@qq.com
 * @author Administrator
 * @date Nov 13, 2016 1:52:44 AM
 */
@Service
public class TUserServiceImpl implements TUserService {

	// protected Log log = LogFactory.getLog(getClass());
	@Autowired
	private TUserDao userDao;// 注入TUser的dao

	
	public int insert(TUser user) {
		user.setPassword(MD5.encode(user.getPassword()));
		user.setCreateTime(WebUtil.getDateTimestamp());
		user.setUserBalance(0.0);
		user.setCreditAmt(0.0);
		user.setUserStatus(1l);
		return userDao.insert(user);
	}

	
	public int deleteByPrimaryKey(Long userId) {
		return userDao.deleteByPrimaryKey(userId);
	}

	
	public int updateByPrimaryKey(TUser user) {
		return userDao.updateByPrimaryKey(user);
	}

	
	public TUser selectByPrimaryKey(Long userId) {
		return userDao.selectByPrimaryKey(userId);
	}

	
	public List<TUser> selectList(Map<String, Object> map) {
		return userDao.selectList(map);
	}

	
	public PageBean<TUser> selectListByPage(Map<String, Object> map) {
		Pager.addPage(map);
		Integer count = userDao.selectListCount(map);
		List<TUser> list = userDao.selectList(map);
		PageBean<TUser> pageList = new PageBean<TUser>(list);
		pageList.setTotal(count);
		pageList.setList(list);
		pageList.setPageNum((Integer) map.get("pageNum"));
		pageList.setPageSize((Integer) map.get("pageSize"));
		pageList.setPages(Pager.getPages(count, (Integer) map.get("pageSize")));
		return pageList;
	}

	
	public TUser selectByModelSelective(TUser user) {
		return userDao.selectByModelSelective(user);
	}

	
	public TUser login(TUser u) {
		TUser up = new TUser();
		up.setUserNo(u.getUserNo());
		TUser resultUser = userDao.selectByModelSelective(up);
		if (resultUser != null) {
			if (MD5.encode(u.getPassword()).equals(resultUser.getPassword())) {
				return resultUser;
			}
		}
		return null;
	}
	
	
	public int selectListCount(Map<String, Object> map){
		return userDao.selectListCount(map);
	}

	
	public void insertList(List<TUser> list) {
		// TODO Auto-generated method stub
		
	}

	
	public PageBean<TUser> selectUserList(Map<String, Object> map) {
		Pager.addPage(map);
		Integer count = userDao.selectListCount(map);
		List<TUser> list = userDao.selectUserList(map);
		PageBean<TUser> pageList = new PageBean<TUser>(list);
		pageList.setTotal(count);
		pageList.setList(list);
		pageList.setPageNum((Integer) map.get("pageNum"));
		pageList.setPageSize((Integer) map.get("pageSize"));
		pageList.setPages(Pager.getPages(count, (Integer) map.get("pageSize")));
		return pageList;
	}

	
	public void deleteAllByUserId(Long userId) {
		userDao.deleteAllByUserId(userId);
		
	}
	
	
	public void updateAllUserByParent(Long userId){
		userDao.updateAllUserByParent(userId);
	}
}
